//---------------------------------------------------------------------------


#pragma hdrstop

#include <string.h>

#include "translateSina.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#include <stdio.h>
char null_str0[256];
char *parseStr;

int getNextItem(char *str_i, int *offset_o, char *str_o)
{
  int offsetCount = 0;

  if (str_i[*offset_o] == 0)
  {
      str_o[offsetCount] = 0;
      return 0;
  }
  while (str_i[*offset_o] != ',' && str_i[*offset_o] != 0)
  {
    str_o[offsetCount] = str_i[*offset_o];
    offsetCount++;
    (*offset_o)++;
  }
  if (str_i[*offset_o] == ',')
    (*offset_o)++;
  str_o[offsetCount] = 0;
  
  return 1;
}

translateStock(char *str_i, StockRecord *stockRecord_o)
{
    int i = 0;
    char tempString[256];
    sscanf(str_i, "%s%s", &null_str0, &parseStr);
    while (parseStr[i] != '\"')
        i++;
    i++;

    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // stock name
        strcpy(stockRecord_o->name, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // open
        sscanf(tempString, "%f", &stockRecord_o->open);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // last close
        sscanf(tempString, "%f", &stockRecord_o->lastClose);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // close
        sscanf(tempString, "%f", &stockRecord_o->close);
    }
    return 0;
}

translateStock(char *str_i, StockRecordString *stockRecord_o)
{
  int i = 0, numOfRecords = 0;
  char tempString[256];

  parseStr = str_i + 4; // skip the "var "
  while (parseStr[i] != 0)
  {
    while (parseStr[i] != '\"')
        i++;
    i++;

    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // stock name
        strcpy(stockRecord_o->name, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // open
        strcpy(stockRecord_o->open, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // last close
        strcpy(stockRecord_o->lastClose, tempString);
    }
    if (getNextItem(parseStr, &i, tempString) == 1)
    {
        // close
        strcpy(stockRecord_o->close, tempString);
    }

    // now skip the CR/LF
    while ((parseStr[i] != '\n') && (parseStr[i] != 0))
      i++;
    if (parseStr[i] == '\n')
      i++;
    numOfRecords++;
    stockRecord_o++;
  }
  return numOfRecords;

}
